//
//  HomeMianViewController.m
//  HTP
//
//  Created by admin on 15/10/30.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "HomeMianViewController.h"
#import "HJCarouselViewLayout.h"
#import "MainCollectionViewCell.h"
#import "AppDelegate.h"
@interface HomeMianViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) UICollectionView *mainCollectionView;
@end

@implementation HomeMianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(181, 181, 181, 181);
    [self.view addSubview:self.mainCollectionView];
    NSIndexPath *index = [NSIndexPath indexPathForItem:2 inSection:0];
    [self.mainCollectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

//TODO:UI 重写get方法
-(UICollectionView *)mainCollectionView{
    if (!_mainCollectionView) {
        HJCarouselViewLayout *hJCarouseLayout = [[HJCarouselViewLayout alloc] initWithAnim:HJCarouselAnimLinear];
        hJCarouseLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        hJCarouseLayout.itemSize = CGSizeMake( ScreenWidth - 60 , ScreenWidth-20);
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - NavHe - 49) collectionViewLayout:hJCarouseLayout];
        _mainCollectionView.backgroundColor = RGBCOLOR(200, 200, 200, 1);
        _mainCollectionView.dataSource = self;
        _mainCollectionView.delegate = self;
        _mainCollectionView.showsHorizontalScrollIndicator = NO;
        _mainCollectionView.showsVerticalScrollIndicator = NO;
        [_mainCollectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"mainCollectionCell"];
    }
    return _mainCollectionView;
}
//TODO:collection delegate datasoucre
- (NSIndexPath *)curIndexPath {
    NSArray *indexPaths = [self.mainCollectionView indexPathsForVisibleItems];
    NSIndexPath *curIndexPath = nil;
    NSInteger curzIndex = 0;
    for (NSIndexPath *path in indexPaths.objectEnumerator) {
        UICollectionViewLayoutAttributes *attributes = [self.mainCollectionView layoutAttributesForItemAtIndexPath:path];
        if (!curIndexPath) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
            continue;
        }
        if (attributes.zIndex > curzIndex) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
        }
    }
    return curIndexPath;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (indexPath.row == curIndexPath.row) {
        return YES;
    }
    
    [self.mainCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"click %ld", indexPath.row);
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
/**
 *  需要自定义cell
 *
 *  @param collectionView
 *  @param indexPath
 *
 *  @return
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCollectionCell" forIndexPath:indexPath];
    cell.layer.borderWidth = 1.0;
    cell.layer.borderColor = [UIColor blackColor].CGColor;
    cell.backImg.image = [UIImage imageNamed:@"backGround"];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
